package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.JwtUtils;
import com.itheima.common.PageBean;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * The type Emp service.
 */
@Service
@RequiredArgsConstructor
public class EmpServiceImpl implements EmpService {
    private final EmpMapper empMapper;

    @Override
    public PageBean<Emp> page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {

        PageBean<Emp> pageBean = new PageBean<>();

        // 查数量
//        Long count = empMapper.selectCount(name, gender, begin, end);
//
//        if (count > 0) {
//            // 查数据
//            // 索引计算
//            int index = (page - 1) * pageSize;
//            List<Emp> empList = empMapper.selectList(name, gender, begin, end, index, pageSize);
//            pageBean.setRows(empList);
//        }
//
//        pageBean.setTotal(count);
//
//        return pageBean;
        PageHelper.startPage(page, pageSize);
        List<Emp> empList = empMapper.selectList(name, gender, begin, end);
        // 类型转换
        Page<Emp> empPage = (Page<Emp>) empList;
        long count = empPage.getTotal();
        List<Emp> result = empPage.getResult();

        pageBean.setRows(result);
        pageBean.setTotal(count);
        return pageBean;

    }

    @Override
    public void removeBatch(List<String> ids) {
        empMapper.removeBatch(ids);
    }

    @Override
    public void create(Emp emp) {

        // 用户名唯一，不能为空
        if (!StringUtils.hasText(emp.getUsername())) {
            throw new RuntimeException("用户名不能为空");
        }
        if (emp.getUsername().length() <2 || emp.getUsername().length()>20) {
            throw new RuntimeException("用户名长度不合法");
        }
        Integer countByUsername = empMapper.selectCountByUsername(emp.getUsername());
        if (countByUsername>0) {
            throw new RuntimeException("用户名重复");
        }

        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp queryById(Integer id) {
        Emp emp =  empMapper.selectById(id);
        emp.setPassword(null);
        return emp;
    }

    @Override
    public void update(Emp emp) {
        if (Objects.isNull(emp.getId())){
            throw new RuntimeException("请求非法");
        }
        if (!StringUtils.hasText(emp.getUsername())) {
            throw new RuntimeException("用户名不能为空");
        }
        if (emp.getUsername().length() <2 || emp.getUsername().length()>20) {
            throw new RuntimeException("用户名长度不合法");
        }
        Integer countByUsername = empMapper.selectCountByUsernameNotId(emp.getUsername(),emp.getId());
        if (countByUsername>0) {
            throw new RuntimeException("用户名重复");
        }
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
    }

    @Override
    public String login(Emp emp) {
       Emp empByUsername =  empMapper.selectOneByUsername(emp.getUsername());
        if (Objects.isNull(empByUsername)) {
            throw new RuntimeException("账号不存在");
        }
        if (!Objects.equals(emp.getPassword(),empByUsername.getPassword())) {
            throw new RuntimeException("密码不正确");
        }

        HashMap<String,Object> map = new HashMap<>();
        map.put("id",empByUsername.getId());
        map.put("username",empByUsername.getUsername());
        return JwtUtils.generateJwt(map);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void deleteByDeptId(Integer deptId) {
        empMapper.deleteByDeptId(deptId);


    }

}
